﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CSharpProjectLayers.DAL
{
    /// <summary>
    /// 通用数据访问类
    /// </summary>
    class SQLHelper
    {
        //读取配置文件获得连接字符串
        private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();
        /// <summary>
        /// 执行insert、update、delete类型的SQL语句
        /// </summary>
        /// <param name="cmdText">SQL语句或存储过程名称</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string cmdText, SqlParameter[] paramArray = null)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(cmdText, conn);
            if (paramArray != null)
            {
                cmd.Parameters.AddRange(paramArray);
            }
            try
            {
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                string errorMsg = $"{DateTime.Now}: + 执行public int ExecuteNonQuery(string cmdText, SqlParameter[] paramArray= null)方法发生了异常: {ex.Message}";
                //在这个地方写入日志

                throw new Exception("执行public int ExecuteNonQuery(string cmdText, SqlParameter[] paramArray= null)方法发生了异常:" + ex.Message);
            }
            finally //以上不管是否发生异常，都会执行的代码
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 返回单一结果的查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string cmdText, SqlParameter[] paramArray = null)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(cmdText, conn);
            if (paramArray != null)
            {
                cmd.Parameters.AddRange(paramArray);
            }
            try
            {
                conn.Open();
                return cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                string errorMsg = $"{DateTime.Now}: + 执行public object ExecuteScalar(string cmdText, SqlParameter[] paramArray= null)方法发生了异常: {ex.Message}";
                //在这个地方写入日志

                throw new Exception(errorMsg);
            }
            finally //以上不管是否发生异常，都会执行的代码
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 返回一张只读结果集的查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string cmdText, SqlParameter[] paramArray = null)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(cmdText, conn);
            if(paramArray != null)
            {
                cmd.Parameters.AddRange(paramArray);
            }
            try
            {
                conn.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection); //必须添加这个枚举
            }
            catch (Exception ex)
            {
                string errorMsg = $"{DateTime.Now}: + 执行 public SqlDataReader ExecuteReader(string cmdText, SqlParameter[] paramArray = null)方法发生了异常: {ex.Message}";
                //在这个地方写入日志             
                throw new Exception(errorMsg);
            }
        }
        /// <summary>
        /// 返回一个包含一张数据表的数据集的查询
        /// </summary>
        public static DataSet GetDataSet(string sql, string tableName = null)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                if (tableName == null)
                    da.Fill(ds);
                else
                    da.Fill(ds, tableName);
                return ds;
            }
            catch (Exception ex)
            {
                string errorMsg = $"{DateTime.Now}: + 执行public static DataSet GetDataSet(string sql, string tableName = null)方法发生了异常: {ex.Message}";
                //在这个地方写入日志

                throw new Exception(errorMsg);
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 执行查询，返回一个或多个表的DataSet
        /// </summary>
        public static DataSet GetDataSet(Dictionary<string, string> dicTableAndSql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                foreach (string tbName in dicTableAndSql.Keys)
                {
                    cmd.CommandText = dicTableAndSql[tbName];
                    da.Fill(ds, tbName);
                }
                return ds;
            }
            catch (Exception ex)
            {
                string errorMsg = $"{DateTime.Now}: + 执行public static DataSet GetDataSet(string sql, string tableName = null)方法发生了异常: {ex.Message}";
                //在这个地方写入日志

                throw new Exception(errorMsg);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}

